from django.contrib import admin
from django.contrib.auth.models import User, Permission as DjangoPermission
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import Group
from .models import Article, Category, ArticleCategory, Department, PermissionGroup, Permission, UserGroup, UserGroupMembership, ArticleReview, CategoryReviewProcess

# 注册文章模型
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'status', 'created_at', 'published_at')
    list_filter = ('status', 'published', 'created_at', 'published_at')
    search_fields = ('title', 'content')
    readonly_fields = ('created_at', 'updated_at', 'submitted_at', 
                       'first_reviewed_at', 'final_reviewed_at', 'published_at', 'withdrawn_at')
    
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.select_related('author')

# 注册分类模型
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'parent', 'description', 'created_at')
    list_filter = ('parent',)
    search_fields = ('name', 'code', 'description')
    ordering = ('parent__name', 'name')  # 按父分类和名称排序

# 注册文章分类模型
@admin.register(ArticleCategory)
class ArticleCategoryAdmin(admin.ModelAdmin):
    list_display = ('article', 'category')
    list_filter = ('category',)

# 注册部门模型
@admin.register(Department)
class DepartmentAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'parent', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('name', 'code', 'description')

# 注册Django自带的权限模型
@admin.register(DjangoPermission)
class DjangoPermissionAdmin(admin.ModelAdmin):
    list_display = ('name', 'content_type', 'codename')
    list_filter = ('content_type',)
    search_fields = ('name', 'codename')
    ordering = ('content_type', 'name')

# 注册Group模型
@admin.register(Group)
class GroupAdmin(admin.ModelAdmin):
    list_display = ('name',)
    search_fields = ('name',)
    filter_horizontal = ('permissions',)

# 注册权限组模型
@admin.register(PermissionGroup)
class PermissionGroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('name', 'code', 'description')
    filter_horizontal = ('permissions',)

# 注册权限模型（自定义）
@admin.register(Permission)
class CustomPermissionAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'module', 'created_at')
    list_filter = ('module', 'created_at')
    search_fields = ('name', 'code', 'description')

# 注册用户组模型
@admin.register(UserGroup)
class UserGroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'department', 'is_active', 'created_at')
    list_filter = ('is_active', 'department', 'created_at')
    search_fields = ('name', 'code', 'description')
    filter_horizontal = ('permission_groups', 'users')

# 注册用户组成员关系模型
@admin.register(UserGroupMembership)
class UserGroupMembershipAdmin(admin.ModelAdmin):
    list_display = ('user', 'user_group', 'role', 'joined_at')
    list_filter = ('user_group', 'role')
    search_fields = ('user__username', 'user_group__name')

# 注册文章审核记录模型
@admin.register(ArticleReview)
class ArticleReviewAdmin(admin.ModelAdmin):
    list_display = ('article', 'reviewer', 'action', 'status_before', 'status_after', 'created_at')
    list_filter = ('action', 'created_at', 'reviewer')
    search_fields = ('article__title', 'reviewer__username')

# 注册栏目审核流程配置模型
@admin.register(CategoryReviewProcess)
class CategoryReviewProcessAdmin(admin.ModelAdmin):
    list_display = ('category', 'review_level', 'first_reviewer_group', 'final_reviewer_group', 'auto_publish', 'is_active')
    list_filter = ('review_level', 'auto_publish', 'is_active')
